package com.suixun.controller.credits;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.authority.controller.BaseManager;
import com.suixun.entity.form.credits.BasePageForm;
import com.suixun.entity.form.credits.company.GoodsArchivesPageForm;
import com.suixun.entity.form.credits.company.GoodsPageForm;
import com.suixun.service.credits.GoodsArchivesService;
import com.suixun.service.credits.PutAreaService;
import com.suixun.util.Constant;
import com.suixun.vo.credits.GoodsArchivesVo;
import com.suixun.vo.credits.ResponseVo;

/**
 * 商品档案
 * 
 * @author yh
 * @date 2017年12月13日
 */
@RestController
@RequestMapping(value = "/credits/goods/archives")
public class GoodsArchivesController extends BaseManager {

    @Autowired
    private GoodsArchivesService goodsArchivesService;

    @Autowired
    private PutAreaService putAreaService;

    /**
     * 分页查询商品列表
     * 
     * @param param
     *            查询条件
     * @return
     */
    @RequestMapping(value = "/find_page.json", method = RequestMethod.GET)
    public Object findPage(GoodsArchivesPageForm param) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (!param.verify()) {
                return fail(Constant.PARAM_ERROR);
            }
            return success(goodsArchivesService.findPage(param));
        } catch (Exception e) {
            logger("GoodsArchivesController findPage:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 添加
     * 
     * @param param
     * @return
     */
    @RequestMapping(value = "/insert.json", method = RequestMethod.POST)
    public Object insert(GoodsArchivesVo param) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (!param.verify()) {
                return fail(Constant.PARAM_ERROR);
            }
            goodsArchivesService.insert(param, getUserId().intValue());
            return success();
        } catch (Exception e) {
            logger("GoodsArchivesController insert:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 详情查询
     * 
     * @param id
     *            商品id
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    @RequestMapping(value = "/find_details.json", method = RequestMethod.GET)
    public Object findDetails(@RequestParam("id") Integer id) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        if (id == null || id < 1) {
            return fail(Constant.PARAM_ERROR);
        }
        try {
            ResponseVo vo = new ResponseVo<>();
            vo.setList(putAreaService.findByArchivesId(id));
            vo.setData(goodsArchivesService.findDetails(id));
            return success(vo);
        } catch (Exception e) {
            logger("GoodsArchivesController findDetails:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 修改
     * 
     * @param param
     * @return
     */
    @RequestMapping(value = "/update.json", method = RequestMethod.POST)
    public Object update(GoodsArchivesVo param) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (!param.verifyUpdate()) {
                return fail(Constant.PARAM_ERROR);
            }
            goodsArchivesService.update(param);
            return success();
        } catch (Exception e) {
            logger("GoodsArchivesController update:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 删除
     * 
     * @param id
     * @return
     */
    @RequestMapping(value = "/delete.json", method = RequestMethod.GET)
    public Object delete(@RequestParam("id") Integer id) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/
        if (id == null || id < 1) {
            return fail(Constant.PARAM_ERROR);
        }
        try {
            goodsArchivesService.delete(id);
            return success();
        } catch (Exception e) {
            logger("GoodsArchivesController delete:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 商品审核
     * 
     * @param opinion
     *            审核意见
     * @param isPass
     *            是否通过,true通过
     * @param id
     *            商品id
     * @return
     */
    @RequestMapping(value = "/audit_archives.json", method = RequestMethod.POST)
    public Object auditArchives(@RequestParam("opinion") String opinion, @RequestParam("isPass") Boolean isPass,
                                @RequestParam("id") Integer id) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (id == null || StringUtils.isEmpty(opinion) || opinion.length() > 500 || isPass == null) {
                return fail(Constant.PARAM_ERROR);
            }
            goodsArchivesService.auditArchives(opinion, isPass, id, getUserId().intValue());
            return success();
        } catch (Exception e) {
            logger("CompanyController auditArchives:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 分页查询商户下的商品列表
     * 
     * @param param
     *            查询条件
     * @return
     */
    @RequestMapping(value = "/find_page_company.json", method = RequestMethod.GET)
    public Object findPageByCompany(BasePageForm param, @RequestParam("companyid") Integer companyid) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (!param.verify() || companyid == null) {
                return fail(Constant.PARAM_ERROR);
            }
            return success(goodsArchivesService.findPageByCompany(param, companyid));
        } catch (Exception e) {
            logger("GoodsArchivesController findPageByCompany:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 统计商品使用情况
     * 
     * @param archivesid
     *            商品id
     * @return
     */
    @RequestMapping(value = "/count_goods_info.json", method = RequestMethod.GET)
    public Object countGoodsInfo(@RequestParam("archivesid") Integer archivesid) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (archivesid == null) {
                return fail(Constant.PARAM_ERROR);
            }
            return success(new ResponseVo<>(goodsArchivesService.countGoodsInfo(archivesid)));
        } catch (Exception e) {
            logger("GoodsArchivesController countGoodsInfo:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 商品使用详情列表查询
     * 
     * @param archivesid
     *            商品id
     * @return
     */
    @RequestMapping(value = "/find_goods_page.json", method = RequestMethod.GET)
    public Object findGoodsPage(GoodsPageForm param) {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            if (!param.verify()) {
                return fail(Constant.PARAM_ERROR);
            }
            return success(goodsArchivesService.findGoodsPage(param));
        } catch (Exception e) {
            logger("GoodsArchivesController findGoodsPage:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    /**
     * 查询所有商品名称
     * 
     * @param param
     * @return
     */
    @RequestMapping(value = "/find_all_name.json", method = RequestMethod.GET)
    public Object findAllName() {

        /*if (!checkRight()) {
            return fail(Constant.Authority.CHECK_FALSE);
        }*/

        try {
            return success(new ResponseVo<>(goodsArchivesService.findAllName()));
        } catch (Exception e) {
            logger("GoodsArchivesController findAllName:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }

    
    @RequestMapping(value = "/getGoods.json", method = RequestMethod.GET)
    public Object getGoods(String goodsNumbers) {

        try {
            return success(goodsArchivesService.listGoodsArchives(goodsNumbers));
        } catch (Exception e) {
            logger("GoodsArchivesController getGoods:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }
    
    @RequestMapping(value = "/top.json", method = RequestMethod.POST)
    public Object top(Integer id, Boolean isTop) {
        try {
        	if(id == null) {
        		return fail("id不能为空");
        	}
        	goodsArchivesService.top(id, isTop);
            return success();
        } catch (Exception e) {
        	e.printStackTrace();
            logger("GoodsArchivesController top:" + e.getMessage(), e);
            return fail(Constant.EXCEPTION.OPERATE_FAIL);
        }
    }
}
